package cn.csc.module.shop.controller.admin.recommend;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.excel.core.util.ExcelUtils;
import cn.csc.framework.operatelog.core.annotations.OperateLog;
import cn.csc.module.shop.controller.admin.recommend.vo.*;
import cn.csc.module.shop.convert.recommend.ShopRecommendConvert;
import cn.csc.module.shop.dal.dataobject.recommend.ShopRecommendDO;
import cn.csc.module.shop.service.recommend.ShopRecommendService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 店铺推荐记录")
@RestController
@RequestMapping("/shop/recommend")
@Validated
public class ShopRecommendController {

    @Resource
    private ShopRecommendService recommendService;

    @PostMapping("/create")
    @ApiOperation("创建店铺推荐记录")
    @PreAuthorize("@ss.hasPermission('shop:recommend:create')")
    public CommonResult<BigInteger> createRecommend(@Valid @RequestBody ShopRecommendCreateReqVO createReqVO) {
        return success(recommendService.createRecommend(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新店铺推荐记录")
    @PreAuthorize("@ss.hasPermission('shop:recommend:update')")
    public CommonResult<Boolean> updateRecommend(@Valid @RequestBody ShopRecommendUpdateReqVO updateReqVO) {
        recommendService.updateRecommend(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除店铺推荐记录")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('shop:recommend:delete')")
    public CommonResult<Boolean> deleteRecommend(@RequestParam("id") BigInteger id) {
        recommendService.deleteRecommend(id);
        return success(true);
    }

    @DeleteMapping("/deleteByShopId")
    @ApiOperation("删除店铺推荐记录")
    @ApiImplicitParam(name = "shopId", value = "店铺编号", required = true, dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('shop:recommend:delete')")
    public CommonResult<Boolean> deleteRecommendByShopId(@RequestParam("shopId") BigInteger shopId) {
        recommendService.deleteRecommendByShopId(shopId);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得店铺推荐记录")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('shop:recommend:query')")
    public CommonResult<ShopRecommendRespVO> getRecommend(@RequestParam("id") BigInteger id) {
        ShopRecommendDO recommend = recommendService.getRecommend(id);
        return success(ShopRecommendConvert.INSTANCE.convert(recommend));
    }

    @GetMapping("/list")
    @ApiOperation("获得店铺推荐记录列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('shop:recommend:query')")
    public CommonResult<List<ShopRecommendRespVO>> getRecommendList(@RequestParam("ids") Collection<BigInteger> ids) {
        List<ShopRecommendDO> list = recommendService.getRecommendList(ids);
        return success(ShopRecommendConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得店铺推荐记录分页")
    @PreAuthorize("@ss.hasPermission('shop:recommend:query')")
    public CommonResult<PageResult<ShopRecommendRespVO>> getRecommendPage(@Valid ShopRecommendPageReqVO pageVO) {
        PageResult<ShopRecommendDO> pageResult = recommendService.getRecommendPage(pageVO);
        return success(ShopRecommendConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/listOfLatest")
    @ApiOperation("获得最新的店铺推荐记录")
    @PermitAll
    public CommonResult<List<ShopRecommendDO>> getRecommendListOfLatest(@Valid ShopRecommendListReqVO reqVO) {
        List<ShopRecommendDO> list = recommendService.getRecommendListOfLatest(reqVO);
        return success(list);
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出店铺推荐记录 Excel")
    @PreAuthorize("@ss.hasPermission('shop:recommend:export')")
    @OperateLog(type = EXPORT)
    public void exportRecommendExcel(@Valid ShopRecommendExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ShopRecommendDO> list = recommendService.getRecommendList(exportReqVO);
        // 导出 Excel
        List<ShopRecommendExcelVO> datas = ShopRecommendConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "店铺推荐记录.xls", "数据", ShopRecommendExcelVO.class, datas);
    }

}
